<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>出版社管理</el-breadcrumb-item>
    </el-breadcrumb>



    <el-divider></el-divider>

    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="编号" width="50" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="300" align="center"></el-table-column>
      <el-table-column label="操作" width="220" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="openDelectConfirm(scope.row)"></el-button>
          <el-button type="primary" icon="el-icon-circle-plus-outline" circle size="mini" @click="insertP()"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改" :visible.sync="dialogFormVisible">
      <el-form :model="pack">
        <el-form-item label="编号" label-width="80px">
          <el-input v-model="pack.id" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="名称" label-width="80px">
          <el-input v-model="pack.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update()">修 改</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加" :visible.sync="dialogFormVisible1">
      <el-form :model="pack1">
        <el-form-item label="名称" label-width="80px">
          <el-input v-model="pack1.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="insertPress()">添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
      pack:{id:1,name:11},
      pack1:{name:11},
      select1:{},
      ruleForm: {

      },
      dialogTableVisible: false,
      dialogFormVisible: false,

      dialogTableVisible1: false,
      dialogFormVisible1: false,
    }
  },
  methods:{
    handleEdit(pack) {
      this.pack.id=pack.id;
      this.pack.name=pack.name
      this.dialogFormVisible = true
    },
    insertP() {
      this.dialogFormVisible1 = true
    },
    insertPress(){
      let url='http://localhost:8080/adminGoodsAddNew/insetPress';
      let pack1=this.qs.stringify(this.pack1)
      console.log("pack1=",pack1)
      this.axios.post(url,pack1).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.$message({
            type: 'success',
            message: "添加成功"
          });
          this.dialogFormVisible1=false
          this.loadGoodsList();
        }
      })
    },
    update(){
      let url='http://localhost:8080/adminGoodsAddNew/updatePressName';
      let pack1=this.qs.stringify(this.pack)
      console.log("pack=",pack1)
      this.axios.post(url,pack1).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.$message({
            type: 'success',
            message: "修改成功"
          });
          this.dialogFormVisible=false
          this.loadGoodsList();
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
          this.loadGoodsList();
        }
      })
    },
    handleDelect(pack){
      console.log("准备删除"+pack.id)
      let url='http://localhost:8080/adminGoodsAddNew/'+pack.id+'/deletePress';
      this.axios.post(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.loadGoodsList();
          this.$message({
            type: 'success',
            message: '删除成功'
          });
          this.loadGoodsList();
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
          this.loadGoodsList();
        }
      })
    },
    openDelectConfirm(brand){
      let message='此操作将永久删除该出版社《'+brand.name+'》, 是否继续???';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelect(brand);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    loadGoodsList(){
      console.log('packlist')
      let url='http://localhost:8080/adminGoodsAddNew/presslist';
      console.log('url='+url);
      this.axios.get(url).then((response)=>{
        let responseBody=response.data;
        this.tableData=responseBody.data;
      })
    }
  },
  mounted() {//挂载
    console.log('mounted')
    this.loadGoodsList();
  }

}
</script>